package com.example.webweatherapplication

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.webweatherapplication.weather.Forecast
import com.example.webweatherapplication.weather.Weather
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class MainActivity2 : AppCompatActivity() {

    // 基础 URL，用于获取天气信息
    val baseURL = "http://t.weather.itboy.net/api/weather/city/"

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge() // 启用边缘到边缘的显示
        setContentView(R.layout.activity_main2) // 设置布局文件

        // 获取布局中的控件
        val textView_city = findViewById<TextView>(R.id.textView_city)
        val textView_shidu = findViewById<TextView>(R.id.textView_shidu)
        val textView_wendu = findViewById<TextView>(R.id.textView_wendu)
        val textView_provience = findViewById<TextView>(R.id.textView_provience)
        val imageView = findViewById<ImageView>(R.id.imageView)
        val listView = findViewById<ListView>(R.id.listView)

        // 获取传递过来的城市代码
        val cityCode = intent.getStringExtra("city_code")
        val queue = Volley.newRequestQueue(this) // 创建请求队列

        // 创建网络请求
        val stringRequest = StringRequest(baseURL + cityCode, { response ->
            val gson = Gson()
            val WeatherType = object : TypeToken<Weather>() {}.type
            val weather = gson.fromJson<Weather>(response, WeatherType) // 解析 JSON 数据

            // 设置文本内容
            textView_city.text = weather.cityInfo.city
            textView_provience.text = weather.cityInfo.parent
            textView_shidu.text = weather.data.shidu
            textView_wendu.text = weather.data.wendu

            // 根据天气类型设置图片
            val firstDay = weather.data.forecast.first()
            when (firstDay.type) {
                "晴" -> imageView.setImageResource(R.drawable.sun)
                "阴" -> imageView.setImageResource(R.drawable.cloud)
                "多云" -> imageView.setImageResource(R.drawable.mcloud)
                "阵雨" -> imageView.setImageResource(R.drawable.thunder)
                "下雪" -> imageView.setImageResource(R.drawable.snow)
                "大雨" -> imageView.setImageResource(R.drawable.rain)
            }

            // 创建适配器，将天气预报数据与简单列表项布局关联
            val adapter = ArrayAdapter<Forecast>(this, android.R.layout.simple_list_item_1, weather.data.forecast)
            listView.adapter = adapter  // 设置适配器给 ListView，使其显示天气预报数据

            // 打印日志
            Log.d("MainActivity2", "${weather.cityInfo.city} ${weather.cityInfo.parent}")
        }, { error ->
            Log.d("MainActivity2", "$error") // 打印错误日志
        })

        queue.add(stringRequest) // 添加请求到队列
    }
}